package hva.team2.viavia;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainMenu extends Activity {

    private class StableArrayAdapter extends ArrayAdapter<String> {
        // http://www.vogella.com/articles/AndroidListView/#listview_overview
        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
        private int selectedItem = -1;
        public StableArrayAdapter(Context context, int layout, int textViewResourceId, List<String> objects) {
            super(context,layout, textViewResourceId, objects);
            for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
            }
        }

        @Override
        public long getItemId(int position) {
            String item = getItem(position);
            return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            if (position % 2 == 1) {
                view.setBackgroundColor(Color.rgb(0,80,239));
            } else {
                view.setBackgroundColor(Color.rgb(0,171,169));
            }


            return view;
        }

    }


    private ListView menu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu);


        menu = (ListView) findViewById(R.id.menuList);

        String[] values = new String[]{"Link with Bike", "Cities", "Create Route",
                "Help", "Options"};

        ArrayList<String> titles = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
            titles.add(values[i]);
        }

        final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_row, R.id.menu_title,titles);
       // final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_row, titles);
        menu.setAdapter(adapter);
        menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
               final String item = (String) parent.getItemAtPosition(position);
               Log.e("ITEM", item);
               adapter.selectedItem = position;

            }

        });


    }


    

}
